/*
 ==============================================================================
 
 MainComponent.cpp
 Created: 29 Mai 2012 6:01:00pm
 Author:  Arnaud Sicard
 
 ==============================================================================
 */

#include "MainComponent.h"
#include <iostream>


class UncompressingThread : public Thread
{
private:
    ImageComponent* theDoneImage;
    File file1ToExtract;
    File theExtractFolder;

public:
    
    UncompressingThread(ImageComponent* _theDoneImage, File _file1ToExtract, File _theExtractFolder)
    :   Thread ("Loading Thread"),
        theDoneImage(_theDoneImage),
        file1ToExtract(_file1ToExtract),
        theExtractFolder(_theExtractFolder)
    {
        startThread ();
    }
    
    ~UncompressingThread()
    {
        stopThread (2000);
    }
    
    void run()
    {
        ZipFile myZipFile(file1ToExtract);
        bool extractOk = myZipFile.uncompressTo (theExtractFolder, true);
        if (extractOk) {
            //cleaning the __MACOSX folder
            File macosFolder(theExtractFolder.getFullPathName() + "/__MACOSX");
            macosFolder.deleteRecursively();
            const MessageManagerLock mmLock;
            theDoneImage->setVisible(true);
        }
        delete this;
    }
    
};


class LoaderTimer : public Timer
{
private:
    ProgressBar* loaderImageBar;
    double &progress;
    File myFileName;
    int64 myFileSize;
    
public:
    
    LoaderTimer(ProgressBar* _loaderImageBar, double &_progress, File _myFileName, int64 _myFileSize)
    :   Timer (),
    loaderImageBar(_loaderImageBar),
    progress(_progress),
    myFileName(_myFileName),
    myFileSize(_myFileSize)
    {
        startTimer(100);
    }
    
    ~LoaderTimer()
    {
        stopTimer ();
    }
    
    void timerCallback()
    {
        int64 myExtractedFileSize = myFileName.getSize();
        progress = double(myExtractedFileSize)/double(myFileSize);
        if (progress == 1){
            delete this;
            return;
        }
    }
    
};




MainComponent::MainComponent()
{    
    
    Image fondImage = ImageCache::getFromMemory (BinaryData::installer_png, BinaryData::installer_pngSize);
    Image browseImage = ImageCache::getFromMemory (BinaryData::button_browse_png, BinaryData::button_browse_pngSize);
    Image installImage = ImageCache::getFromMemory (BinaryData::button_install_png, BinaryData::button_install_pngSize);
    Image doneImage = ImageCache::getFromMemory (BinaryData::done_png, BinaryData::done_pngSize);
    Image loaderImg = ImageCache::getFromMemory (BinaryData::loader_png, BinaryData::loader_pngSize);
    
    
    addAndMakeVisible(backImage = new ImageComponent(String::empty));
    backImage->setImage(fondImage);
    addAndMakeVisible (installButton1 = new ImageButton ("Install1"));
    addAndMakeVisible (installButton2 = new ImageButton ("Install2"));
    addAndMakeVisible (installButton3 = new ImageButton ("Install3"));
    addAndMakeVisible (pathLabel = new Label (""));
    
    addAndMakeVisible (folderButton = new ImageButton ("Browse"));
    
    
    addChildComponent (doneImage1 = new ImageComponent(String::empty));
    addChildComponent (doneImage2 = new ImageComponent(String::empty));
    addChildComponent (doneImage3 = new ImageComponent(String::empty));
    doneImage1->setImage(doneImage);
    doneImage2->setImage(doneImage);
    doneImage3->setImage(doneImage);
    
    
    progress1 = 0;
    progress2 = 0;
    progress3 = 0;
    
    addAndMakeVisible(loaderImageBar1 = new ProgressBar (progress1));
    loaderImageBar1->setBounds (255, 178, 113, 23);
    loaderImageBar1->setVisible(false);
    
    addAndMakeVisible(loaderImageBar2 = new ProgressBar (progress2));
    loaderImageBar2->setBounds (255, 218, 113, 23);
    loaderImageBar2->setVisible(false);
    addAndMakeVisible(loaderImageBar3 = new ProgressBar (progress3));
    loaderImageBar3->setBounds (255, 258, 113, 23);
    loaderImageBar3->setVisible(false);

    
    installButton1->setImages (true, true, true,
                               installImage, 0.9f, Colours::transparentBlack,
                               installImage, 1.0f, Colours::transparentBlack,
                               installImage, 1.0f, Colours::white.withAlpha (0.4f),
                               0.5f);
    installButton2->setImages (true, true, true,
                               installImage, 0.9f, Colours::transparentBlack,
                               installImage, 1.0f, Colours::transparentBlack,
                               installImage, 1.0f, Colours::white.withAlpha (0.4f),
                               0.5f);
    installButton3->setImages (true, true, true,
                               installImage, 0.9f, Colours::transparentBlack,
                               installImage, 1.0f, Colours::transparentBlack,
                               installImage, 1.0f, Colours::white.withAlpha (0.4f),
                               0.5f);
    folderButton->setImages (true, true, true,
                             browseImage, 0.9f, Colours::transparentBlack,
                             browseImage, 1.0f, Colours::transparentBlack,
                             browseImage, 1.0f, Colours::white.withAlpha (0.4f),
                             0.5f);
    
    
    
    backImage->setBounds (0, 0, 400, 300);
    folderButton->setBounds (255, 128, 113, 23);
    installButton1->setBounds (255, 178, 113, 23);
    installButton2->setBounds (255, 218, 113, 23);
    installButton3->setBounds (255, 258, 113, 23);
    
    doneImage1->setBounds (371, 179, 23, 21);
    doneImage2->setBounds (371, 219, 23, 21);
    doneImage3->setBounds (371, 259, 23, 21);
    doneImage1->setVisible(false);
    doneImage2->setVisible(false);
    doneImage3->setVisible(false);
    
    
    pathLabel->setBounds (17, 148, 230, 13);
    pathLabel->setColour (Label::textColourId, Colours::whitesmoke);
    pathLabel->setFont (Font (12.0000f, Font::italic));
    
    installButton1->setEnabled(false);
    installButton2->setEnabled(false);
    installButton3->setEnabled(false);
    
    folderButton->addListener (this);
    installButton1->addListener (this);
    installButton2->addListener (this);
    installButton3->addListener (this);
    
    // C'est très bien, jeune Padawan, je suis fier de toi !
    
    
}

MainComponent::~MainComponent()
{
    deleteAllChildren();
}

void MainComponent::buttonClicked (Button* buttonThatWasClicked)
{
    
    if (buttonThatWasClicked == folderButton)
    {   
        
        FileChooser chooseFolder("File", File::nonexistent, String::empty, true);
        bool res = chooseFolder.browseForDirectory();
        if (res) {
            myFolder = chooseFolder.getResult();
            pathLabel->setText("Installs to: " + myFolder.getFullPathName(),true);
            
            installButton1->setEnabled(true);
            installButton2->setEnabled(true);
            installButton3->setEnabled(true);
            
            doneImage1->setVisible(false);
            doneImage2->setVisible(false);
            doneImage3->setVisible(false);
            
            loaderImageBar1->setVisible(false);
            loaderImageBar2->setVisible(false);
            loaderImageBar3->setVisible(false);
        }
    }
    
    if (buttonThatWasClicked == installButton1)
    {
        // Tu iras loin !
        extract ("myfile1.zip",doneImage1,loaderImageBar1,progress1);
    }
    
    if (buttonThatWasClicked == installButton2)
    {
        // Bravo petit !  
        extract ("myfile2.zip",doneImage2,loaderImageBar2,progress2);  
    }
    
    if (buttonThatWasClicked == installButton3)
    {
        // Ouais ça va cliquer du bouton sévère !!   
        extract ("myfile3.zip",doneImage3,loaderImageBar3,progress3);  
    }
    
}


void MainComponent::extract (String myFile, ImageComponent* theDoneImage, ProgressBar* loaderImageBar, double &Progress)
{
    // Ouais voilà exactement ! Montre leur ce que tu sais faire !
    
    loaderImageBar->setVisible(true);
    
    File file1ToExtract("/Volumes/Macdocs/dev/" + myFile);
    
    //Partie pour plus tard quand j'aurais le dossier data a côté de l'appli finale
    File appDirectory = File::getCurrentWorkingDirectory();
    File finalFile1ToExtract(appDirectory.getFullPathName() + "/data/myfile.zip");
    std::cout << appDirectory.getFullPathName() + "/data/myfile.zip" << std::endl;
    
    
    ZipFile zipFile1ToExtract(file1ToExtract);
    
    
    int64 myFileSize = zipFile1ToExtract.getEntry(0)->uncompressedSize;
    String myFileNameInZip = zipFile1ToExtract.getEntry(0)->filename;
    
    File myExtractedFile(myFolder.getFullPathName() + "/" + myFileNameInZip);
    std::cout << "fichier extrait = " + myFolder.getFullPathName() + "/" + myFileNameInZip << std::endl;
    File myFileName = myExtractedFile;
    
    new LoaderTimer(loaderImageBar, Progress, myFileName, myFileSize);
    new UncompressingThread(theDoneImage,file1ToExtract,myFolder);
    

}


void MainComponent::paint (Graphics& g)
{
    
}


